package com.dotstone.chipism.adapter;

import java.util.List;

import com.dotstone.chipism.R;
import com.dotstone.chipism.activity.MainActivity;
import com.dotstone.chipism.bean.Device;
import com.dotstone.chipism.bean.DeviceType;
import com.dotstone.chipism.bean.MySocket;
import com.dotstone.chipism.util.SocketManager;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainDeviceAdapter extends BaseAdapter {
	private Context context;
	private List<Device> data;
	private Handler mHandler;

	public MainDeviceAdapter(Context context) {
		this.context = context;
	}

	public MainDeviceAdapter(Context context, List<Device> data) {
		this.data = data;
		this.context = context;
	}

	public void setHandler(Handler mHandler) {
		this.mHandler = mHandler;
	}

	public void setData(List<Device> data) {
		this.data = data;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void clear() {
		this.data.clear();
		notifyDataSetChanged();
	}

	public void addAll(List<Device> data) {
		if (data != null) {
			this.data.addAll(data);
			notifyDataSetChanged();
		}
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		if (view == null) {
			view = LayoutInflater.from(context).inflate(
					R.layout.item_main_device, null);
			holdView = new HoldView();
			holdView.deviceTv = (TextView) view.findViewById(R.id.name_tv);
			holdView.mToggleButton = (ToggleButton) view
					.findViewById(R.id.onoff_tgl);
			holdView.mToggleL = (LinearLayout) view.findViewById(R.id.toggle_l);
			holdView.state = (TextView)view.findViewById(R.id.state_tv);
			view.setTag(holdView);
		} else {
			holdView = (HoldView) view.getTag();
		}
		final Device d = data.get(position);
		holdView.deviceTv.setText(d.getDeviceName());
		if (d.getDeviceType() == DeviceType.DEVICE_TYPE_RGB_LIGHT
				|| d.getDeviceType() == DeviceType.DEVICE_TYPE_RGB_TIAO
				|| d.getDeviceType() == DeviceType.DEVICE_TYPE_SOCKET
				|| d.getDeviceType() == DeviceType.DEVICE_TYPE_SW_LIGHT
				|| d.getDeviceType() == DeviceType.DEVICE_TYPE_SWITCH) {
			holdView.mToggleButton.setVisibility(View.VISIBLE);
		} else {
			holdView.mToggleL.setVisibility(View.GONE);
		}
		if(d.getDeviceType()==DeviceType.DEVICE_TYPE_WLAN){
			MySocket socket = SocketManager.getInstance().sMap.get(d.getDeviceID());
			if(socket!=null){
				if(socket.isLocalSuccess()){
					holdView.state.setText("本地");
				}else if(!socket.isLocalSuccess()&&SocketManager.getInstance().isRemoteSuccess()){
					if(SocketManager.getInstance().mStatusMap.get(d.getDeviceID())!=null){
						if (SocketManager.getInstance().mStatusMap.get(d.getDeviceID())==1) {
							holdView.state.setText("远程");
						}else{
							holdView.state.setText("未连接");
						}
					}else{
						holdView.state.setText("未连接");
					}
				}else{
					holdView.state.setText("未连接");
				}
			}
		}
		holdView.mToggleL.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Log.i("wmy", "消费点击");
			}
		});
		holdView.mToggleButton
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton arg0,
							boolean isCheck) {
						Message msg = Message.obtain();
						msg.what = MainActivity.SINGLE_DEVICE_CTRL;
						msg.arg1 = position;
						msg.arg2 = d.getDeviceType();
						msg.obj = isCheck;
						mHandler.sendMessage(msg);
					}
				});
		view.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent e) {
				// TODO Auto-generated method stub
				switch (e.getAction()) {
				case MotionEvent.ACTION_DOWN:
					Log.i("wmy", "按下e.getX() = " + e.getX() + " e.getY() = "
							+ e.getY());

					break;
				case MotionEvent.ACTION_UP:
					Log.i("wmy", "抬起e.getX() = " + e.getX() + " e.getY() = "
							+ e.getY());
					break;

				default:
					break;
				}
				return false;
			}
		});
		return view;
	}

	HoldView holdView;

	class HoldView {
		private TextView deviceTv;
		private ToggleButton mToggleButton;
		private LinearLayout mToggleL;
		private TextView state;
	}

}